using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NTTHAO.Web.BL.CommonPage;
using NTTHAO.CommonWeb;

public partial class admin_webparts_Plug_ins_CommonPage_CommonPageMenuContent : System.Web.UI.UserControl
{
    public static readonly int PAGE_SIZE = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["NUM_ROW_PER_PAGE"]);
    public int _PageNo = 1;
    public int _PageCount = 1;
    public long _TotalItem = -1;
    public string _PageUrl = "";

    public CommonMenu _SelectedMenu = null;
    public DataTable _ContentList = null;
    public DataTable _AlbumList = null;
    public bool _IsAdminLogged = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this._IsAdminLogged = "admin".Equals(Context.User.Identity.Name, StringComparison.OrdinalIgnoreCase);

            int menuID = 0;
            try
            {
                menuID = Int32.Parse(Request.QueryString["mnuid"].Trim());
            }
            catch (Exception)
            {
                //Response.Redirect("?type=invalidurl", false);
                return;
            }

            if (Request.QueryString["page"] != null)
            {
                try
                {
                    _PageNo = Convert.ToInt32(Request.QueryString["page"].Trim());
                }
                catch { }
            }
            try
            {
                for (int i = 0; i < Request.QueryString.Count; i++)
                {
                    if (!"page".Equals(Request.QueryString.GetKey(i), StringComparison.OrdinalIgnoreCase)
                        && !"time".Equals(Request.QueryString.GetKey(i), StringComparison.OrdinalIgnoreCase))
                    {
                        _PageUrl += "&" + Request.QueryString.GetKey(i) + "=" + Server.UrlEncode(Request.QueryString[i]);
                    }
                }
                if (_PageUrl.Length > 0)
                {
                    _PageUrl = "?" + _PageUrl.TrimStart('&');
                }
            }
            catch { }

            if (menuID > 0)
            {
                this._SelectedMenu = new CommonMenu(menuID);

                if (this._SelectedMenu.ID < 1)
                {
                    this._SelectedMenu = null;
                    return;
                }
                if (this._SelectedMenu.LinkType == 1)
                {
                    //CommonAlbum album = new CommonAlbum();
                    //ds = album.GetAllByMenuID(this._SelectedMenu.ID);
                    DataSet ds = CommonAlbum.GetPageByMenuID(this._SelectedMenu.ID, -1, false, 0, ContentSortTypes.DefaultThenInsertTimeDecrease, _PageNo, PAGE_SIZE, ref _TotalItem);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        this._AlbumList = ds.Tables[0];
                    }
                }
                else
                {
                    DataSet ds = CommonPageContent.GetPageByMenuID(this._SelectedMenu.ID, -1, false, 0, ContentSortTypes.DefaultThenInsertTimeDecrease, _PageNo, PAGE_SIZE, ref _TotalItem);
                    //DataSet ds = content.GetPageByMenuID(this._SelectedMenu.ID, -1, _PageNo, PAGE_SIZE);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        this._ContentList = ds.Tables[0];
                    }
                }

                this._PageCount = (int)(this._TotalItem / PAGE_SIZE + (this._TotalItem % PAGE_SIZE != 0 ? 1 : 0));
                this._PageCount = this._PageCount < 1 ? 1 : this._PageCount;
            }
            _PageUrl += (_PageUrl.Length > 0 ? "&" : "?") + "time=" + DateTime.Now.Ticks.ToString() + "&";
        }
        catch (Exception) { }
    }
}
